using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BoringTodo
{
    public partial class EmailConfigForm : Form
    {
        public string SmtpServer { get; private set; }
        public int SmtpPort { get; private set; }
        public string EmailAddress { get; private set; }
        public string EmailPassword { get; private set; }
        public bool EnableSsl { get; private set; }
        public string RecipientEmail { get; private set; }

        private TextBox txtSmtpServer;
        private TextBox txtPort;
        private TextBox txtEmailAddress;
        private TextBox txtPassword;
        private TextBox txtRecipientEmail;
        private CheckBox chkEnableSsl;
        private Button btnOK;
        private Button btnCancel;
        private Label lblSmtpServer;
        private Label lblPort;
        private Label lblEmail;
        private Label lblPassword;
        private Label lblRecipient;
        private Label lblSsl;
        private Label lblInfo;

        public EmailConfigForm()
        {
            InitializeComponent();
            
            // 加载默认常用SMTP服务器设置
            LoadDefaultSettings();
        }

        private void LoadDefaultSettings()
        {
            // 预设Gmail的设置
            txtSmtpServer.Text = "smtp.gmail.com";
            txtPort.Text = "587";
            chkEnableSsl.Checked = true;
            
            // 添加常用邮件服务器的提示信息
            string tip = "常用SMTP设置:\n" +
                      "Gmail: smtp.gmail.com, 端口587, SSL启用\n" +
                      "Outlook: smtp.office365.com, 端口587, SSL启用\n" +
                      "QQ邮箱: smtp.qq.com, 端口587, SSL启用\n" +
                      "注意：Gmail和其他服务可能需要应用专用密码";
            
            this.lblInfo.Text = tip;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            // 验证输入
            if (string.IsNullOrWhiteSpace(txtSmtpServer.Text) ||
                string.IsNullOrWhiteSpace(txtEmailAddress.Text) ||
                string.IsNullOrWhiteSpace(txtPassword.Text) ||
                string.IsNullOrWhiteSpace(txtRecipientEmail.Text))
            {
                MessageBox.Show("请填写所有必填项。", "输入验证", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // 验证端口
            if (!int.TryParse(txtPort.Text, out int port) || port <= 0 || port > 65535)
            {
                MessageBox.Show("请输入有效的端口号 (1-65535)。", "输入验证", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // 验证邮箱格式
            if (!IsValidEmail(txtEmailAddress.Text) || !IsValidEmail(txtRecipientEmail.Text))
            {
                MessageBox.Show("请输入有效的邮箱地址。", "输入验证", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // 保存设置
            SmtpServer = txtSmtpServer.Text;
            SmtpPort = port;
            EmailAddress = txtEmailAddress.Text;
            EmailPassword = txtPassword.Text;
            EnableSsl = chkEnableSsl.Checked;
            RecipientEmail = txtRecipientEmail.Text;

            DialogResult = DialogResult.OK;
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private bool IsValidEmail(string email)
        {
            try
            {
                var addr = new System.Net.Mail.MailAddress(email);
                return addr.Address == email;
            }
            catch
            {
                return false;
            }
        }
    }
} 